package com.xbongbong.paas.controller.mobile;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.thread.TransmittableThreadLocalUtil;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.common.pojo.CheckRepeatToolPojo;
import com.xbongbong.pro.common.pojo.dto.MobileCheckRepeatToolDTO;
import com.xbongbong.pro.common.pojo.vo.CheckRepeatToolVO;
import com.xbongbong.pro.constant.XbbProConstant;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.pro.weblist.pojo.dto.RepeatToolFieldGetDTO;
import com.xbongbong.pro.weblist.pojo.vo.RepeatFieldListVO;
import com.xbongbong.saas.service.RecheckingService;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Objects;

/**
 * @author 杨磊
 * @ProjectName xbb-pro
 * @Description: 移动端重复规则相关接口
 * @date 2022/11/14 15:34
 */
@RestController
@RequestMapping(value = XbbProConstant.API_ROOT_PREFIX + "/mobile/rechecking")
public class MobileRecheckingController {

    @Resource
    private RecheckingService recheckingService;

    /**
     * @description 查重工具获取查重参数
     * @author yanglei
     * @date 2022/12/2 14:16
     * @param repeatToolFieldGetDTO
     * @param br
     * @return String
     */
    @RequestMapping(value = "repeatTool/repeatField", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String getRepeatField(@RequestBody @Valid RepeatToolFieldGetDTO repeatToolFieldGetDTO, BindingResult br) {
        XbbResponse<RepeatFieldListVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                RepeatFieldListVO repeatFieldListVO = recheckingService.getRepeatField(repeatToolFieldGetDTO);
                response = new XbbResponse<>(repeatFieldListVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    /**
     * @description 常用查重
     * @author yanglei
     * @date 2022/12/2 14:17
     * @param checkRepeatToolDTO
     * @param br
     * @return String
     */
    @RequestMapping(value = "repeatTool/searchCommonRecheck", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String searchCommonRecheck(@RequestBody @Valid MobileCheckRepeatToolDTO checkRepeatToolDTO, BindingResult br) throws Exception {
        XbbResponse<CheckRepeatToolVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                TransmittableThreadLocalUtil.setValue(checkRepeatToolDTO.getSaasMark(), checkRepeatToolDTO.getBusinessType(), null, checkRepeatToolDTO.getDistributorMark());
                CheckRepeatToolVO checkRepeatToolVO = recheckingService.mobileToolCheckRepeat(checkRepeatToolDTO);
                response = new XbbResponse<>(checkRepeatToolVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    /**
     * @description 常用查重结果集中 通过切换具体表单
     * @author yanglei
     * @date 2022/12/2 14:17
     * @param checkRepeatToolDTO
     * @param br
     * @return String
     */
    @RequestMapping(value = "/repeatTool/searchCommonRecheckByFormId", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String searchCommonRecheckByFormId(@RequestBody @Valid MobileCheckRepeatToolDTO checkRepeatToolDTO, BindingResult br) throws Exception {
        XbbResponse<CheckRepeatToolPojo> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                TransmittableThreadLocalUtil.setValue(checkRepeatToolDTO.getSaasMark(), checkRepeatToolDTO.getBusinessType(), null, checkRepeatToolDTO.getDistributorMark());
                CheckRepeatToolPojo checkRepeatToolPojo = recheckingService.mobileToolCheckRepeatByFormId(checkRepeatToolDTO);
                response = new XbbResponse<>(checkRepeatToolPojo);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    /**
     * @description 高级查重
     * @author yanglei
     * @date 2022/12/2 14:17
     * @param seniorReCheckSearchDTO
     * @param br
     * @return String
     */
    @RequestMapping(value = "repeatTool/searchSeniorRecheck", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String searchSeniorRecheck(@RequestBody @Valid MobileCheckRepeatToolDTO seniorReCheckSearchDTO, BindingResult br) throws Exception {
        XbbResponse<CheckRepeatToolVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                TransmittableThreadLocalUtil.setValue(seniorReCheckSearchDTO.getSaasMark(), seniorReCheckSearchDTO.getBusinessType(), null, seniorReCheckSearchDTO.getDistributorMark());
                CheckRepeatToolVO checkRepeatToolVO = recheckingService.mobileToolCheckRepeat(seniorReCheckSearchDTO);
                response = new XbbResponse<>(checkRepeatToolVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    /**
     * @description 高级查重-单模板查重
     * @author yanglei
     * @date 2022/12/2 14:17
     * @param seniorReCheckSearchDTO
     * @param br
     * @return String
     */
    @RequestMapping(value = "repeatTool/searchSeniorRecheckByFormId", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String searchSeniorRecheckByFormId(@RequestBody @Valid MobileCheckRepeatToolDTO seniorReCheckSearchDTO, BindingResult br) throws Exception {
        XbbResponse<CheckRepeatToolPojo> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                TransmittableThreadLocalUtil.setValue(seniorReCheckSearchDTO.getSaasMark(), seniorReCheckSearchDTO.getBusinessType(), null, seniorReCheckSearchDTO.getDistributorMark());
                CheckRepeatToolPojo checkRepeatToolVO = recheckingService.mobileToolCheckRepeatByFormId(seniorReCheckSearchDTO);
                response = new XbbResponse<>(checkRepeatToolVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }


}
